<script setup>
  import { ref } from 'vue'
  import { User, Lock } from '@element-plus/icons-vue'
  import { ElMessage } from 'element-plus'
  import { useRouter } from 'vue-router'
  import { useUserStore } from '@/stores/user'
  import { uploadingApi } from '@/apis/uploading'
  import { useUploadStore } from '@/stores/upload'

  // 上传数据
  const uploadStore = useUploadStore()
  const uploading = async () => {
    if (!uploadStore.hasUploaded) {
      await uploadingApi()
      uploadStore.setUploaded(true)
    }
  }
  uploading()

  // 登录字段
  const router = useRouter()
  const formModel = ref({
    username: '',
    password: ''
  })

  const rememberMe = ref(false)
  const form = ref(null)

  // 表单校验
  const rules = {
    username: [
      { required: true, message: '请输入用户名', trigger: 'blur' },
      { min: 1, trigger: 'blur' }
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      { min: 6, max: 10, message: '长度在 6 到 18 个字符', trigger: 'blur' }
    ]
  }
  // 登录
  const handleLogin = async () => {
    await form.value.validate()
    const userStore = useUserStore()
    await userStore.getUserInfo(formModel.value)
    ElMessage.success('登录成功')
    router.push('/')
  }
</script>

<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-content">
        <el-form
          :model="formModel"
          :rules="rules"
          ref="form"
          size="large"
          autocomplete="off"
          class="login-form"
        >
          <el-form-item>
            <div class="login-title">
              <h1>系统登录</h1>
              <p>欢迎回来，请登录您的账户</p>
            </div>
          </el-form-item>
          <el-form-item prop="username">
            <el-input
              v-model="formModel.username"
              :prefix-icon="User"
              placeholder="请输入用户名"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="formModel.password"
              :prefix-icon="Lock"
              type="password"
              placeholder="请输入密码"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item class="form-actions">
            <div class="remember-forgot">
              <el-checkbox v-model="rememberMe">记住我</el-checkbox>
              <el-link type="primary">忘记密码？</el-link>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button
              @click="handleLogin"
              class="login-button"
              type="primary"
              auto-insert-space
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style scoped>
  .login-container {
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #f5f5f5;
  }

  .login-box {
    position: relative;
    width: 500px;
    padding: 40px;
    border-radius: 12px;
  }

  .login-content {
    background-color: #ffffff;
    border-radius: 12px;
    padding: 40px;
    box-shadow: 0 0 30px rgba(0, 0, 0, 0.15);
  }

  .login-title {
    text-align: center;
    margin-bottom: 40px;
  }

  .login-title h1 {
    font-size: 28px;
    color: #333;
    margin-bottom: 15px;
  }

  .login-title p {
    font-size: 16px;
    color: #666;
  }

  .login-form {
    margin-top: 20px;
  }

  .login-button {
    width: 100%;
    height: 52px;
    font-size: 18px;
    margin-top: 20px;
    background-color: #409eff;
    border: none;
  }

  .remember-forgot {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    margin-top: 10px;
  }

  :deep(.el-input__wrapper) {
    height: 52px;
    border-radius: 8px;
    background-color: #ffffff;
    font-size: 16px;
    border: 1px solid #dcdfe6;
  }

  :deep(.el-input__prefix) {
    display: flex;
    align-items: center;
  }

  :deep(.el-checkbox__label) {
    color: #606266;
    font-size: 15px;
  }

  :deep(.el-link) {
    color: #409eff !important;
    font-size: 15px;
  }
</style>
